﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.SystemConfig.Services;
using Web.SystemConfig.DataAccess;
using System.Data;
using System.Web;
using Microsoft.Web.UI.WebControls;
using DreamBuilder.Common;
using DreamBuilder.Core.Message;

namespace Web.SystemConfig.Business
{
    public class BUSAuth_Menu : BusinessBase, IAuth_Menu
    {
        DALAuth_Menu dal;

        public BUSAuth_Menu()
        {
            dal = new DALAuth_Menu();
        }
        #region IAuth_Menu 成员
        public string CheckMenuParentId(string parrentId)
        {
            DataTable dt = dal.CheckMenuParentId(parrentId);
            string parentid = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                parentid = dt.Rows[0][0].ToString();
            }
            return parentid;
        }

        public DataSet GetDeleteChildInfo(string pkValue)
        {
            return dal.GetDeleteChildInfo(pkValue);
        }

        public ResultInfo DeleteMenu(string pkValue)
        {
            ResultInfo result = new ResultInfo();
            result = dal.DeleteMenu(pkValue);
            return result;
        }

        public string GetFuncName(string Func_Name_ID)
        {
            DataTable dt = dal.GetFuncName(Func_Name_ID);
            string funcName = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                funcName = dt.Rows[0][0].ToString();
            }
            return funcName;
        }


        #region 生成树

        private DataTable dtOrg;

        public DataTable DtOrg
        {
            get
            {
                if (dtOrg == null)
                {
                    dtOrg = dal.GetAllMenuData();
                }
                return dtOrg;
            }
        }

        public void CreateTree(TreeView tree)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_Menu_ID is null ");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                else
                {
                    DataRow rootDr = arrDr[0];
                    TreeNode root = new TreeNode();
                    root.ID = rootDr["Menu_ID"].ToString();
                    root.Text = rootDr["Menu_Name"].ToString();
                    //string url = "../SystemConfig/Auth_Menu.aspx?Parent_Menu_ID=" + rootDr["Menu_ID"].ToString();

                    root.NavigateUrl = "Auth_Menu.aspx?Parent_Menu_ID=" + rootDr["Menu_ID"].ToString();
                    tree.Nodes.Add(root);
                    AddChildNode(root, rootDr["Menu_ID"].ToString());
                }
            }

        }

        private void AddChildNode(TreeNode root, string parentID)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_Menu_ID='" + parentID + "'");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDr)
                {
                    TreeNode child = new TreeNode();
                    child.ID = dr["Menu_ID"].ToString();
                    child.Text = dr["Menu_Name"].ToString();
                    //string url = "../SystemConfig/Auth_Menu.aspx?Parent_Menu_ID=" + dr["Menu_ID"].ToString();
                    //loadingpage 为进度条
                    child.NavigateUrl = "Auth_Menu.aspx?Parent_Menu_ID=" + dr["Menu_ID"].ToString();
                    root.Nodes.Add(child);
                    AddChildNode(child, dr["Menu_ID"].ToString());
                }
            }
        }

        #endregion

        #endregion
        #region 保存菜单多语信息 add by lyn 20120503
        /// <summary>
        /// 保存菜单多语信息
        /// </summary>
        /// <param name="menuID">菜单ID</param>
        /// <param name="menuDesc">菜单描述</param>       
        /// <returns></returns>
        public ResultInfo SaveMenuDesc(string menuID, string menuDesc)
        {
            ResultInfo result = new ResultInfo();
            int menuNum = dal.IsMenuID(menuID);
            if (menuNum > 0)
            {
               result= dal.UpdateMenuDescInfo(menuID, menuDesc);
            }
            else
            {
                result = dal.SaveMenuDescInfo(menuID, menuDesc);
            }
           
            return result;
        }
        #endregion
        #region 删除菜单多语信息 add by lyn 20120503
        /// <summary>
        /// 删除菜单多语信息
        /// </summary>
        /// <param name="pkValue">菜单ID</param>               
        /// <returns></returns>
        public ResultInfo DeleteMenuDesc(string pkValue)
        {
            ResultInfo result = new ResultInfo();
            result = dal.DeleteMenuDescInfo(pkValue);
            return result;
        }
        #endregion    

        public string GetMenuDesc(string menuid)
        {
            return dal.GetMenuDesc(menuid);
        }
        /// <summary>
        /// 根据菜单链接获取菜单menuId
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetMenuIdByUrl(string url)
        {
            return dal.GetMenuIdByUrl(url);
        }
    }
}
